package com.template.templateuser.domain.mapper;

import com.template.templateuser.domain.dto.SysDeptDTO;
import com.template.templateuser.domain.dto.SysUserDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;

import java.util.List;

public interface SysUserMapper {
    @Select({
            "select * from sys_user"
    })
    List<SysUserDTO> getSysUserList();
    @Select({
            "<script>",
            "select count(*) from sys_user ",
            "<where> ",
            "<if test=\"sysUserDTO.loginName != null  and sysUserDTO.loginName !='' \" > ",
            " or ( login_name= #{sysUserDTO.loginName} and deleted=0)",
            "  </if> ",
            "<if test=\"sysUserDTO.email != null  and sysUserDTO.email !='' \" > ",
            " or ( email= #{sysUserDTO.email} and deleted=0)",
            "  </if> ",
            "<if test=\"sysUserDTO.phonenumber != null  and sysUserDTO.phonenumber !='' \" > ",
            " or ( phonenumber= #{sysUserDTO.phonenumber} and deleted=0)",
            "  </if> ",
            "</where>",
            "</script>",
    })
    int queryByContion(@Param("sysUserDTO") SysUserDTO sysUserDTO);




    /**
     * 保存
     * @param sysUserDTO
     * @return
     */
    @Insert({
            "<script>",
            "INSERT INTO sys_user",
            "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\" >",
            "   <if test=\"deptId != null\" >",
            "       dept_id,",
            "   </if>",
            "   <if test=\"loginName != null\" >",
            "       login_name,",
            "   </if>",
            "   <if test=\"userName != null\" >",
            "       user_name,",
            "   </if>",
            "   <if test=\"userType != null\" >",
            "       user_type,",
            "   </if>",
            "   <if test=\"email != null\" >",
            "       email,",
            "   </if>",
            "   <if test=\"phonenumber != null\" >",
            "       phonenumber,",
            "   </if>",
            "   <if test=\"sex != null\" >",
            "       sex,",
            "   </if>",
            "   <if test=\"deleted != null\" >",
            "       deleted,",
            "   </if>",
            "   <if test=\"avatar != null\" >",
            "       avatar,",
            "   </if>",
            "   <if test=\"createTime != null\" >",
            "       create_time,",
            "   </if>",
            "   <if test=\"updateTime != null\" >",
            "       update_time,",
            "   </if>",
            "   <if test=\"password != null\" >",
            "       password,",
            "   </if>",
            "   <if test=\"status != null\" >",
            "       status,",
            "   </if>",
            "</trim>",
            "<trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\" >",
            "   <if test=\"deptId != null\" >",
            "       #{deptId},",
            "   </if>",
            "   <if test=\"loginName != null\" >",
            "       #{loginName},",
            "   </if>",
            "   <if test=\"userName != null\" >",
            "       #{userName},",
            "   </if>",
            "   <if test=\"userType != null\" >",
            "       #{userType},",
            "   </if>",
            "   <if test=\"email != null\" >",
            "       #{email},",
            "   </if>",
            "   <if test=\"phonenumber != null\" >",
            "       #{phonenumber},",
            "   </if>",
            "   <if test=\"sex != null\" >",
            "       #{sex},",
            "   </if>",
            "   <if test=\"deleted != null\" >",
            "       #{deleted},",
            "   </if>",
            "   <if test=\"avatar != null\" >",
            "       #{avatar},",
            "   </if>",
            "   <if test=\"createTime != null\" >",
            "       #{createTime},",
            "   </if>",
            "   <if test=\"updateTime != null\" >",
            "       #{updateTime},",
            "   </if>",
            "   <if test=\"password != null\" >",
            "       #{password},",
            "   </if>",
            "   <if test=\"status != null\" >",
            "       #{status},",
            "   </if>",
            "</trim>",
            "</script>",
    })
    @SelectKey(resultType = long.class, keyColumn = "id", before = false, statement = "SELECT LAST_INSERT_ID() AS id",
            keyProperty = "id")
    int saveUser(SysUserDTO sysUserDTO);

    @Select({
            "select * from sys_user where login_name=#{loginName}"
    })
    SysUserDTO findUserByLoginName(@Param("loginName") String loginName);

    int updateUserByUserId(SysUserDTO sysUserDTO);

    @Select({
            "select * from sys_user where id=#{id}"
    })
    SysUserDTO findById(@Param("id") Long id);
}
